from abc import ABC

from rest_framework import serializers
from .models import ProductDetail, Shopcarts, Product, Coupon, CouponUse, Address, ProductAttribute
import datetime

now_time = datetime.datetime.now()


# 商品列表序列化器
class ProductListSerializer(serializers.ModelSerializer):
    product_type = serializers.SerializerMethodField()

    # 重写返回值
    def get_product_type(self, obj):
        return obj.get_product_type_display()

    class Meta:
        model = Product
        fields = ['id', 'product_name', 'product_type', 'show_img', 'descript']


# 商品详情序列化器
class ProductDetailSerializer(serializers.ModelSerializer):
    # 商品属性
    attributes_info = serializers.SerializerMethodField(read_only=True)
    # 切割图片成数组
    product_swiper_img_info = serializers.SerializerMethodField(read_only=True)
    product_detail_img_info = serializers.SerializerMethodField(read_only=True)

    def get_product_swiper_img_info(self, obj):
        pro_swiper = obj.product_swiper_img
        return pro_swiper.split('::')

    def get_product_detail_img_info(self, obj):
        pro_swiper = obj.product_detail_img
        return pro_swiper.split('::')

    def get_attributes_info(self, obj):
        attr_obj_list = obj.productattribute_set.all()
        return [{
            'id': attr_obj.id,
            'attribute': attr_obj.attribute,
            'characteristic': attr_obj.characteristic,
            'attribute_img': attr_obj.attribute_img,
            'stock_quantity': attr_obj.stock_quantity,
            'price': attr_obj.price
        } for attr_obj in attr_obj_list]

    class Meta:
        model = ProductDetail
        fields = '__all__'
        # read_only_fields = ('account_name',)


# 购物车序列化器
class ShopCarsSerializer(serializers.ModelSerializer):
    product_info = serializers.SerializerMethodField(read_only=True)
    product_attribute_info = serializers.SerializerMethodField(read_only=True)

    def get_product_info(self, obj):
        product_obj = obj.product
        return {'id': product_obj.id, 'product_name': product_obj.product_name, 'show_img': product_obj.show_img}

    def get_product_attribute_info(self, obj):
        pro_attr_obj = obj.product_attribute
        return {
            'id': pro_attr_obj.id,
            'attribute': pro_attr_obj.attribute,
            'characteristic': pro_attr_obj.characteristic,
            'price': pro_attr_obj.price
        }

    class Meta:
        model = Shopcarts
        fields = '__all__'
        extra_kwargs = {
            'product': {'write_only': True},
            'product_attribute': {'write_only': True}
        }


# 优惠券序列化器
class CouponSerializer(serializers.ModelSerializer):
    status = serializers.SerializerMethodField()
    coupon = serializers.SerializerMethodField()

    def get_status(self, obj):
        return obj.get_status_display()

    def get_coupon(self, obj):
        coupon_obj = obj.coupon
        return {
            'id': coupon_obj.id,
            'coupon_name': coupon_obj.coupon_name,
            'coupon_type': coupon_obj.get_coupon_type_display(),
            'amount': coupon_obj.amount,
            'min_amount': coupon_obj.min_amount,
            'effective_time': coupon_obj.effective_time,
            'invalid_time': coupon_obj.invalid_time
        }

    class Meta:
        model = CouponUse
        fields = '__all__'


# 地址
class AddressSerializer(serializers.ModelSerializer):
    class Meta:
        model = Address
        fields = '__all__'
        # fields = ['addr_name', 'addr_phone', 'addr_zip', 'addr_province', 'addr_city', 'addr_district', 'is_default']


# 商品
class ProductSerializer(serializers.ModelSerializer):

    class Meta:
        model = Product
        fields = '__all__'


# 商品属性
class ProductAttributeSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductAttribute
        fields = '__all__'
